/**
 * @(#) ReqPacdpiMediator.as
 *
 * @date		2012. 09. 19.
 * @version		1.0 
 * @author		Park, Jutae
 * @description	제증명신청 - Mediator
 * 				제증명 발행을 조회하고 신청하는 Presentation Logic을 관리 
 */
package com.skt.common_hr.pacdp.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.pacdp.PacdpFacade;
	import com.skt.common_hr.pacdp.model.CodesProxy;
	import com.skt.common_hr.pacdp.model.PacdpProxy;
	import com.skt.common_hr.pacdp.model.WorkPlaceProxy;
	import com.skt.common_hr.pacdp.model.vo.PacdpVO;
	import com.skt.common_hr.pacdp.view.components.ReqPacdp;
	import com.wcl.utils.ArrayHelper;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;	

	public class ReqPacdpMediator extends CommonMediator
	{
		public static const NAME:String = "ReqPacdpMediator"; 
		
		private var selectedRec:PacdpVO;
		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function ReqPacdpMediator( viewComponent:Object, obj:PacdpVO )
        {
            super( NAME, viewComponent );
            selectedRec = obj;
            view.callLater(initView);
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		private function initView():void 
 		{
 			if(view.sDvPop == 'S'){
// 				view.cbTrack.dataProvider = view.cbList;
				view.lbENAME.text  = view.sinChungName; 
				view.lbCDPYR.text  = view.sinChungYear;
//				view.lbABIGO.text  = selectedRec.ABIGO;
//				view.lbAPOTXT.text = selectedRec.APOTXT;
				view.cbTrack.enabled = true;
				view.btnSave.visible = true;
				view.btnEsc.label = '취소';	
				
 			}
 			if(view.sDvPop == 'U') {
 				//iCbdata = ArrayHelper.indexOf(view.cbTrack.dataProvider as ArrayCollection, "data", selectedRec.ATRACK); 				
 				view.cbTrack.selectedIndex = ArrayHelper.indexOf(view.cbTrack.dataProvider as ArrayCollection, "data", selectedRec.ATRACK);
 				view.iCbdata = view.cbTrack.selectedIndex;
	 			view.lbCDPYR.text = selectedRec.CDPYR;
	 			view.lbENAME.text = selectedRec.ENAME;
	 			view.lbAPOTXT.text = selectedRec.APOTXT;
	 			view.lbABIGO.text = selectedRec.ABIGO;
	 			view.cbTrack.enabled = true;
	 			view.btnSave.visible = true;
	 			view.btnEsc.label = '취소';
 			}
			if(view.sDvPop == 'R') {
				view.cbTrack.text = selectedRec.ATRTXT;
	 			view.lbCDPYR.text = selectedRec.CDPYR;
	 			view.lbENAME.text = selectedRec.ENAME;
	 			view.lbAPOTXT.text = selectedRec.APOTXT;
	 			view.lbABIGO.text = selectedRec.ABIGO;
	 			
	 			view.cbTrack.enabled = false;
	 			view.btnSave.visible = false;
	 			view.btnEsc.label = '닫기';
			}
 			
 			view.addEventListener(ReqPacdp.OkInput, onOkInput);
 			view.addEventListener(ReqPacdp.CancelInput, onCancelInput);
 		}
 		
 			
 		
 		
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					PacdpFacade.LOAD_CODES_COMPLETE,
					PacdpFacade.LOAD_CODES_FAILED,
					PacdpFacade.LOAD_WORKPLACE_COMPLETE,
					PacdpFacade.LOAD_WORKPLACE_FAILED,
					PacdpFacade.INSERT_COMPLETE,
					PacdpFacade.INSERT_FAILED,
					PacdpFacade.UPDATE_COMPLETE,
					PacdpFacade.UPDATE_FAILED
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case PacdpFacade.LOAD_CODES_COMPLETE:
					reqList(note.getBody() as CodesProxy);
					break;
				case PacdpFacade.LOAD_CODES_FAILED:
					break;
				case PacdpFacade.LOAD_WORKPLACE_COMPLETE:
					var wpProxy:WorkPlaceProxy = note.getBody() as WorkPlaceProxy;
					view.workPlace = wpProxy.place;
					break;
				case PacdpFacade.INSERT_COMPLETE:
					facade.sendNotification(PacdpFacade.OK_REQUEST_INPUT, this);
					break;
				case PacdpFacade.INSERT_FAILED:
					Alert.show((PacdpProxy(note.getBody()).failedAlert));
					facade.sendNotification(PacdpFacade.OK_REQUEST_INPUT, this);
					break;	
				case PacdpFacade.UPDATE_COMPLETE:
					facade.sendNotification(PacdpFacade.OK_REQUEST_INPUT, this);
					break;
				case PacdpFacade.UPDATE_FAILED:
					Alert.show((PacdpProxy(note.getBody()).updateFailed));
					break;	
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		
		/**
		 * Main Module에서 QueryClose 메세지가 왔을 때의 처리
		 *     - 사용자에게 확인하고 View에 종료처리 -> MainModule에 'CLOSE_MODULE' 전송
		 * @param
		 * @return void
		 */
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
 		/**
		 * 모듈 Close 확인 창을 사용자가 확인후 처리
		 *     - Close OK     : View에 종료처리 -> MainModule에 'CLOSE_MODULE' 전송
		 *	   - Close Cancel : 처리 없음 
		 * @param
		 * @return void
		 */

 		private function onOkInput(event:Event):void
 		{
 			if(view.sDvPop == 'U'){
 				var vo:PacdpVO = selectedRec as PacdpVO;
 				vo.ATRACK =  view.cbTrack.selectedItem.data;
 				if(vo.ATRACK == '-1'){
 					Alert.show('Track를 선택해주세요');
 					return ;	
 				}
 				
 				var proxy:PacdpProxy = facade.retrieveProxy(PacdpProxy.NAME) as PacdpProxy;
 				proxy.update(Variables.myPernr, vo);
 			} else {
 				var _vo:PacdpVO = new PacdpVO();
 				_vo.ATRACK =  view.cbTrack.selectedItem.data;
 				if(_vo.ATRACK == '-1'){
 					Alert.show('Track를 선택해주세요');
 					return ;
 				}
 					
 				var proxy1:PacdpProxy = facade.retrieveProxy(PacdpProxy.NAME) as PacdpProxy;
 				proxy1.insert(Variables.myPernr, _vo);	
 			}
 				
 		}
 		
 		private function onCancelInput(event:Event):void
 		{
 			view.removeEventListener(ReqPacdp.CancelInput, onCancelInput);
 			view.removeEventListener(ReqPacdp.OkInput, onOkInput);
 			
 			facade.sendNotification(PacdpFacade.CANCEL_REQUEST_INPUT, this);
 			facade.sendNotification(PacdpFacade.OK_REQUEST_INPUT, this);
 		}
 		
 		public function cancelOK():void
		{
			PopUpManager.removePopUp(view);
		}
 		
 		private function reqList(proxy:CodesProxy):void
 		{
 			view.cbTrack.dataProvider = proxy.cbList;
 			view.lbENAME.text = proxy.sinChungName;
 			view.lbCDPYR.text = proxy.sinChungYear;
 		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():ReqPacdp
        {
            return viewComponent as ReqPacdp;
        }
		
		
	}
}